﻿Imports System.Data
Partial Class WebForm_ManageRole_OpenManageRoleProgramMapping
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub ddlProgramGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProgramGroup.SelectedIndexChanged
        Me.mpSearch.Show()
        Me.ObjectDataSource1.SelectParameters("Parent").DefaultValue = DirectCast(sender, DropDownList).SelectedValue
    End Sub

    Protected Sub cbxFullAccess_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.ChangeData("FullAccess", DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex)
    End Sub
    Protected Sub cbxReadOnly_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.ChangeData("ReadOnly", DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex)
    End Sub
    Protected Sub cbxLimitAccess_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.ChangeData("LimitAccess", DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex)
    End Sub

    'Add By Angus 2011/8/30
    Protected Sub ChangeData(ByVal ChangeType As String, ByVal RowIndex As Integer)
        Try
            Dim cbxReadOnly As CheckBox = TryCast(Me.TGridView1.Rows(RowIndex).FindControl("cbxReadOnly"), CheckBox)
            Dim cbxLimitAccess As CheckBox = TryCast(Me.TGridView1.Rows(RowIndex).FindControl("cbxLimitAccess"), CheckBox)
            Dim cbxFullAccess As CheckBox = TryCast(Me.TGridView1.Rows(RowIndex).FindControl("cbxFullAccess"), CheckBox)
            Dim vReadOnly As Integer = 0
            Dim vLimitAccess As Integer = 0
            Dim vFullAccess As Integer = 0

            Select Case ChangeType
                Case "ReadOnly"
                    If cbxReadOnly.Checked = True Then
                        vReadOnly = 1
                        cbxLimitAccess.Checked = False
                        cbxFullAccess.Checked = False
                    End If
                Case "LimitAccess"
                    If cbxLimitAccess.Checked = True Then
                        vLimitAccess = 1
                        cbxReadOnly.Checked = False
                        cbxFullAccess.Checked = False
                    End If
                Case "FullAccess"
                    If cbxFullAccess.Checked = True Then
                        vFullAccess = 1
                        cbxReadOnly.Checked = False
                        cbxLimitAccess.Checked = False
                    End If
            End Select

            Dim ProgramID As String = TGridView1.DataKeys(RowIndex).Value.ToString()
            Dim sql As New MyStringBuilder
            Dim dat As New clsData
            If vReadOnly + vLimitAccess + vFullAccess = 0 Then
                '都沒勾,就刪除資料
                sql.AppendFormat("Delete from RoleProgramMapping where 1=1 ")
                sql.AppendFormat("and ProgramID='{0}' ", ProgramID)
                sql.AppendFormat("and RoleID='{0}'; ", RoleID)
            Else
                sql.AppendFormat("Update RoleProgramMapping Set ")
                sql.AppendFormat(" ReadOnly='{0}' ", vReadOnly)
                sql.AppendFormat(",LimitAccess='{0}' ", vLimitAccess)
                sql.AppendFormat("where ProgramID='{0}' ", ProgramID)
                sql.AppendFormat("and RoleID='{0}'; ", RoleID)
                'sql.AppendFormat(" AND RoleID in (SELECT Parent FROM Role WHERE RoleID={0}); ", RoleID)
                sql.AppendFormat("Insert into RoleProgramMapping(RoleID,ProgramID,AuthName,ReadOnly,LimitAccess) select ")
                sql.AppendFormat("'{0}' ", RoleID)
                sql.AppendFormat(",'{0}' ", ProgramID)
                sql.AppendFormat(",'{0}' ", "")
                sql.AppendFormat(",'{0}' ", vReadOnly)
                sql.AppendFormat(",'{0}' ", vLimitAccess)
                sql.AppendFormat("where not exists ")
                sql.AppendFormat("( ")
                sql.AppendFormat("  select top 1 RoleID from RoleProgramMapping ")
                sql.AppendFormat("  where RoleID='{0}' ", RoleID)
                'sql.AppendFormat(" where RoleID in (SELECT Parent FROM Role WHERE RoleID={0}) ", RoleID)
                sql.AppendFormat("  and ProgramID='{0}' ", ProgramID)
                sql.AppendFormat("); ")
            End If
            dat.AccData(sql.ToString())
        Catch ex As Exception
            MyObj.WriteLog(ex.ToString())
            MyObj.ShowMessage("Apply Failed!")
        End Try
        Me.mpSearch.Show()
    End Sub

    Protected Sub TGridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TGridView1.PageIndexChanged
        Me.mpSearch.Show()
    End Sub

    Protected Sub TGridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles TGridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)

            If drv("Parent").ToString() = "0" Then
                e.Row.Cells(0).Font.Bold = True
            End If
            'ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(TryCast(e.Row.FindControl("cbxFullAccess"), CheckBox))

        End If
    End Sub

    Public Sub InitDataAndShow()

        Me.mpSearch.Show()
        Me.CheckBoxVisible()
        ddlProgramGroup.SelectedIndex = -1
        Me.ObjectDataSource1.SelectParameters("RoleID").DefaultValue = RoleID.ToString()
        Me.ObjectDataSource1.SelectParameters("Parent").DefaultValue = ddlProgramGroup.SelectedValue
        TGridView1.DataSourceID = "ObjectDataSource1"
        TGridView1.PageIndex = 0
        TGridView1.DataBind()

    End Sub
    Private Sub CheckBoxVisible()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData


        sql.AppendFormat("Update Program Set ")
        sql.AppendFormat("ChkVisible = '0' ")
        sql.AppendFormat("Where ProgramID  in ('10','20','30','113','114','115','116','117','118','119','120'); ")
        sql.AppendFormat("Update Program Set ")
        sql.AppendFormat("ChkVisible = '1' ")
        sql.AppendFormat("Where ProgramID  not in ('10','20','30','113','114','115','116','117','118','119','120'); ")
        Try
            dat.AccData(sql)
        Catch ex As Exception
            MyObj.ShowMessage(ex.Message)
        End Try

    End Sub
    Protected Function BooleanConverter(ByVal strTMP As Object) As Boolean
        Return (IIf(strTMP.ToString() = "1", True, False))
    End Function

    Public Property RoleID() As Integer
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("RoleID") = value
        End Set
    End Property
End Class
